package com.kdg.passwordgenerator;

import javax.imageio.ImageIO;
//import javax.swing.*;
/*import java.awt.*;
import java.awt.event.*;    */
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.prefs.Preferences;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Effect;
import javafx.scene.effect.Glow;
import javafx.scene.effect.SepiaTone;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class GUIMain extends Application {

    public static boolean DEBUG = true;

    public static Date buildDate = new Date();                                //HIDE LATER ONCE RELEASED
    public static DateFormat dFormat = new SimpleDateFormat("yyyy/M/dd");        //HIDE LATER ONCE RELEASED

    private final Settings settingsWindow = new Settings();

    // Menu bar setup
    public static MenuBar menuBar = new MenuBar();
    public static MenuItem settings = new MenuItem("Settings");

    public static String show = "";


    //panels used in a layered card fashion
  /*  private JPanel container;
    public JPanel cards;
    public JPanel card1;
    private JPanel card2;

    //strings used to reference respective cards
    public final static String settingsPanel = "Settings";
    public final static String mainPanelString = "Main";

    private final GUIAbout gAbout = new GUIAbout();
    private final Settings settings = new Settings();
    private final Preferences userPrefs = settings.userPrefs;       */


      /*
    private JMenuItem mExit;
    private JMenuItem mProject;
    private JMenuItem mSettings;
    private JButton btnExit;
    private JButton btnGenerate;

    private JPopupMenu mContext;
    private JMenuItem mCopy;

    private JCheckBox checkClip = Settings.checkClip;
    private JCheckBox checkLetters = Settings.checkLetters;
    private JCheckBox checkNumbers = Settings.checkNumbers;
    private JCheckBox checkSymbols = Settings.checkSymbols;
    private JTextField txtOutput;

    private JTextField txtLimit = Settings.txtLimit;

    private final Misc misc = new Misc();        */

    public GUIMain() {

     /*   this.setTitle("KDG Password Generator 2.0 " + dFormat.format(buildDate));
        int _height = 310;
        int _width = 450;
        this.setMinimumSize(new Dimension(_width, _height));
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setResizable(false);
        gAbout.CreateBody();
        gAbout.setLocationRelativeTo(null);
        settings.CreateBody();

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("KDG_Logo.png"));
        } catch (IOException e) {
                e.printStackTrace();
        }
        setIconImage(image);


    }

    private void CreateMenu() {

        //Menu bar and the headers.
        JMenuBar mBar = new JMenuBar();
        JMenu mFile = new JMenu("File");
        mExit = new JMenuItem("Exit");
        mExit.addActionListener(new EventListener());
        JMenu mAbout = new JMenu("About");
        mProject = new JMenuItem("Project");
        mProject.addActionListener(new EventListener());
        mSettings = new JMenuItem("Settings");
        mSettings.addActionListener(new EventListener());


        //Right click menu.
        mContext = new JPopupMenu();
        mCopy = new JMenuItem("Copy");
        mCopy.addActionListener(new EventListener());
        mContext.add(mCopy);

        //Hotkeys.
        mFile.setMnemonic(KeyEvent.VK_F);
        mAbout.setMnemonic(KeyEvent.VK_A);
        mExit.setMnemonic((KeyEvent.VK_E));
        mProject.setMnemonic(KeyEvent.VK_P);
        mSettings.setMnemonic(KeyEvent.VK_S);
       // mSavePref.setMnemonic(KeyEvent.VK_S);          //REMOVE THESE LATER ONCE WE HAVE SETTINGS PAGE SETUP
       // mResetPref.setMnemonic(KeyEvent.VK_R);         //REMOVE THESE LATER ONCE WE HAVE SETTINGS PAGE SETUP

        //Add main headers to the bar.
        mBar.add(mFile);
        mBar.add(mAbout);

        //Add submenu items to main headers.
        mFile.add(mSettings);
        mFile.add(mExit);
        mAbout.add(mProject);

        setJMenuBar(mBar);

    }

    private void CreateBody() {

        //Layout panels.
        container = new JPanel(new BorderLayout());
        card1 = new JPanel(new BorderLayout());
        card2 = new JPanel(new BorderLayout());


        JPanel mainPanel = new JPanel(new GridLayout(2, 0));       //********* possibly obsolete **************
        JPanel generatePanel = new JPanel(new BorderLayout());     //********* possibly obsolete **************
        JPanel bottomPanel = new JPanel(new FlowLayout());         //********* possibly obsolete **************



        //Buttons.
        btnGenerate = new JButton("Generate");
        btnGenerate.setToolTipText("Each generate will copy to clipboard if selected");
        btnGenerate.addActionListener(new EventListener());
        btnExit = new JButton("Exit");
        btnExit.addActionListener(new EventListener());

        //Output.
        txtOutput = new JTextField();
        txtOutput.setOpaque(false);
        txtOutput.setText("Your generated pass code here");
        txtOutput.setBorder(BorderFactory.createEmptyBorder());
        txtOutput.setBackground(new Color(0, 0, 0, 0));
        txtOutput.setEditable(false);
        txtOutput.setHorizontalAlignment(JTextField.CENTER);
        txtOutput.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    mContext.show(e.getComponent(), e.getX(), e.getY());
                }
            }

            //For Windows compatibility.
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    mContext.show(e.getComponent(), e.getX(), e.getY());
                }
            }

        });






        //generate panel setup.
        generatePanel.add(txtOutput, BorderLayout.CENTER);



        //bottom panel layout.
        bottomPanel.add(btnGenerate);
        bottomPanel.add(btnExit);


        mainPanel.add(generatePanel);
        mainPanel.add(bottomPanel);

        card1.add(mainPanel);

        card2.add(settings, BorderLayout.PAGE_START);

        //main card panel holding cards
        cards = new JPanel(new CardLayout());
        cards.add(card1, mainPanelString);
        cards.add(card2, settingsPanel);

        container.add(cards);


        cl = (CardLayout) (cards.getLayout());

        add(container);

    }

    class EventListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {



            if (e.getSource() == mExit || (e.getSource() == btnExit)) {
                System.exit(0);

            } else if (e.getSource() == mProject) {
                gAbout.setVisible(true);
            }else if (e.getSource() == mSettings){
                cl.show(cards, settingsPanel);
            } else if ((e.getSource() == btnGenerate) && (!checkLetters.isSelected()) && (!checkNumbers.isSelected()) && (!checkSymbols.isSelected())) {
                JOptionPane.showMessageDialog(rootPane, "No checkboxes selected!");

            } else if (e.getSource() == btnGenerate) {
                doRandom();
                if (checkClip.isSelected()) {
                    misc.SetClip(txtOutput.getText());
                }
            } else if (e.getSource()== mCopy) {
                misc.SetClip(txtOutput.getText());

            }

        }
    }

    int getLimit() {

        //Suppressing warning from IDE.
        @SuppressWarnings("UnnecessaryLocalVariable")
        int limit = Integer.parseInt(txtLimit.getText());
        return limit;
    }

    //check limit input by user
    boolean checkLimit() {
        int limit = getLimit();
        return !(limit < 3 || limit > 20);

    }

    public void setCard1Vis(){
        cl.show(cards, mainPanelString);
    }

    private void doRandom() {
        boolean decide = checkLimit();

        if (!decide) {
            JOptionPane.showMessageDialog(rootPane, "Limit must be more than 2 but less than 21");
            txtLimit.setText("7");
        }
        else  {
            misc.SetLimit(getLimit());
            if (checkSymbols.isSelected()) {
                misc.SetSymbols(true);
                txtOutput.setText(misc.GetSymbols());
            }

            if (checkLetters.isSelected()) {
                misc.SetLetters(true);
                txtOutput.setText(misc.GetLetters());
            }

            if (checkNumbers.isSelected()) {
                misc.SetNumbers(true);
                txtOutput.setText(misc.GetNumbers());

            }

            if (checkSymbols.isSelected() && checkLetters.isSelected()) {

                misc.SetLettersSymbols(true);
                txtOutput.setText(misc.GetRandomLettersSymbols());
            }
            if (checkNumbers.isSelected() && checkLetters.isSelected()) {
                misc.SetLettersNumbers(true);
                txtOutput.setText(misc.GetRandomLettersNumbers());
            }

            if (checkSymbols.isSelected() && checkNumbers.isSelected()) {

                misc.SetNumbersSymbols(true);
                txtOutput.setText(misc.GetRandomNumbersSymbols());
            }

            if (checkSymbols.isSelected() && checkLetters.isSelected() && checkNumbers.isSelected()) {
                misc.SetAll(true);
                txtOutput.setText(misc.GetRandomAll());
            }

        }
    }



                        */

    }

    public static void main(String[] args) {
     /*  GUIMain gMain = new GUIMain();

        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        gMain.CreateMenu();
        gMain.CreateBody();
        gMain.setLocationRelativeTo(null);
        gMain.pack();

        //Let the magic flow!
        gMain.setVisible(true);       */
        launch(args);

     }

    @Override
    public void start(final Stage stage) {

        stage.setTitle("KDG Password Generator 2.0 " + dFormat.format(buildDate));     //remove format once released
        Scene scene = new Scene(new VBox(), 400, 300);
        scene.setFill(Color.AQUA);
        stage.getIcons().add(new Image("file:resources/Small_Logo.png"));


        // File menu
        Menu menuFile = new Menu("File");
        MenuItem exit = new MenuItem("Exit");
        menuFile.getItems().addAll(settings, new SeparatorMenuItem(), exit);


        // View menu
        Menu menuView = new Menu("View");
        MenuItem about = new MenuItem("About");
        menuView.getItems().addAll(about);


        // Set menu bar
        menuBar.getMenus().addAll(menuFile, menuView);


        stage.setScene(scene);
        stage.show();


        // Text field
        final TextField passwordField = new TextField("Generated pass code here");
        passwordField.setPrefColumnCount(25);
        passwordField.setEditable(false);
        passwordField.setPrefHeight(100);
        passwordField.setAlignment(Pos.CENTER);

        // Buttons
        Button buttonGenerate = new Button("Generate");
        Button buttonExit = new Button("Exit");

        //Creating a GridPane container
        GridPane grid = new GridPane();
        grid.setVgap(25);
        grid.setHgap(-130);

        GridPane.setConstraints(passwordField, 0, 0);
        grid.getChildren().add(passwordField);
        GridPane.setConstraints(buttonGenerate, 0, 1);
        grid.getChildren().add(buttonGenerate);
        GridPane.setConstraints(buttonExit, 1, 1);
        grid.getChildren().add(buttonExit);

        GridPane.setHalignment(buttonExit, HPos.CENTER);
        GridPane.setHalignment(buttonGenerate, HPos.CENTER);
        GridPane.setHalignment(passwordField, HPos.CENTER);
        GridPane.setMargin(passwordField, new Insets(50,10,10,50));




        /********************************************   EVENT HANDLING  ************************************************************************************/
        about.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {

            }
        });

        buttonExit.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                System.exit(0);
            }
        });
        settings.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                stage.hide();
                settingsWindow.start(new Stage());
            }
        });
        exit.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                System.exit(0);
            }
        });



        ((VBox) scene.getRoot()).getChildren().addAll(menuBar, grid);
        buttonGenerate.requestFocus();

    }



}








